<%+cbi/valueheader%>
<%local fs=require"nixio.fs"%>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" class="cbi-button cbi-button-apply" id="apply_update_button" value="<%:Download%>" onclick=" return apply_update() "/>
<br><br>
<textarea id="core_download" class="cbi-input-textarea" style="width: 100%;display:none" data-update="change" rows="10" cols="100" readonly="readonly" > </textarea>

<script type="text/javascript">//<![CDATA[

function apply_update(){
	XHR.get('<%=luci.dispatcher.build_url("admin", "services", "clash", “doupdate”)%>',null,function(x, data){}
		);
	poll_check();
	return
}

function poll_check(){
var lv = document.getElementById('core_download');
	lv.style.display="inline"
XHR.poll(2, '<%=luci.dispatcher.build_url("admin", "services", "clash", “check”)%>', null,
	function(x, data) {
		scroll();
		var lv = document.getElementById('core_download');
		if (x.responseText && lv) {
			lv.innerHTML += x.responseText; 
		}
	}
);

}

XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "clash", "corelog")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {			
			if(status.downcheck==0||status.downcheck==2){
				hide();	
			}
			if(status.downcheck==1){
				var btn = document.getElementById('apply_update_button');
				btn.disabled = false;
				btn.value    = '<%:Download%>';
				poll_check();
				
			}

		}
});



//]]>

function hide(){
	$('#core_download').hide();
	var btn = document.getElementById('apply_update_button');
	btn.disabled = false;
	btn.value    = '<%:Download%>';
}


function scroll(){
	var textarea = document.getElementById('core_download');
	textarea.scrollTop = textarea.scrollHeight;
}


<% if fs.access("/var/run/core_update") then %>
var	btn = document.getElementById('apply_update_button');
	btn.disabled = true;
	btn.value    = '<%:Downloading...%>';
poll_check();
<%end%>


</script>

<%+cbi/valuefooter%>
